<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    
    <title>
        Java 数字格式化 |
        
        Darklovy</title>
    <meta name="description" content>
    <meta name="keywords" content>
    <meta name="HandheldFriendly" content="True">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <link rel="shortcut icon" href="/">
    <link rel="alternate" href="/atom.xml" title="Darklovy">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="description" content="数字的格式化在解决实际问题时使用非常普遍，如表示某超市的商品价格，不要八六两位有效数字。Java 主要对浮点型数据进行数字格式化操作，其中浮点型数据包括 double 和 float 型数据，在 java 中使用 Java.text.DecimalFormat格式化数字。 DecimalFormat 是 NumberFormat 的一个子类，用于格式化十进制数字。他可以将一些数字格式化为整数、浮点">
<meta name="keywords" content="Java">
<meta property="og:type" content="article">
<meta property="og:title" content="Java 数字格式化">
<meta property="og:url" content="https://www.ttxxly.top/2019/01/10/Java-数字格式化/index.html">
<meta property="og:site_name" content="Darklovy">
<meta property="og:description" content="数字的格式化在解决实际问题时使用非常普遍，如表示某超市的商品价格，不要八六两位有效数字。Java 主要对浮点型数据进行数字格式化操作，其中浮点型数据包括 double 和 float 型数据，在 java 中使用 Java.text.DecimalFormat格式化数字。 DecimalFormat 是 NumberFormat 的一个子类，用于格式化十进制数字。他可以将一些数字格式化为整数、浮点">
<meta property="og:locale" content="en">
<meta property="og:image" content="https://www.ttxxly.top/images/2019-1-10/1.png">
<meta property="og:updated_time" content="2019-01-10T05:46:23.305Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Java 数字格式化">
<meta name="twitter:description" content="数字的格式化在解决实际问题时使用非常普遍，如表示某超市的商品价格，不要八六两位有效数字。Java 主要对浮点型数据进行数字格式化操作，其中浮点型数据包括 double 和 float 型数据，在 java 中使用 Java.text.DecimalFormat格式化数字。 DecimalFormat 是 NumberFormat 的一个子类，用于格式化十进制数字。他可以将一些数字格式化为整数、浮点">
<meta name="twitter:image" content="https://www.ttxxly.top/images/2019-1-10/1.png">

    <link href="https://fonts.googleapis.com/css?family=Inconsolata|Titillium+Web" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css?family=Roboto+Mono" rel="stylesheet">
    <link href="//cdn.bootcss.com/node-waves/0.7.5/waves.min.css" rel="stylesheet">
    <link rel="stylesheet" href="/style.css">
    <script>
        function setLoadingBarProgress(num) {
            document.getElementById('loading-bar').style.width = num + "%";
        }
    </script>
</head>
</html>
<body>
  <div id="loading-bar-wrapper">
  <div id="loading-bar"></div>
</div>


  <script>setLoadingBarProgress(20)</script> 
  <header class="l_header">
	<div class='wrapper'>
		<div class="nav-main container container--flex">
			<a class="logo flat-box" href='/' >
				Darklovy
			</a>
			<div class='menu'>
				<ul class='h-list'>
					
						<li>
							<a class='flat-box nav-Home' href='/'>
								首页
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-archives' href='/archives'>
								归档
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-categories' href='/categories'>
								分类
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-tags' href='/tags'>
								标签
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-algorithm' href='https://book.algorithm.ttxxly.top/'>
								算法
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-python' href='https://book.python.ttxxly.top/'>
								python
							</a>
						</li>
					
						<li>
							<a class='flat-box nav-about' href='/about'>
								关于
							</a>
						</li>
					
				</ul>
				<div class='underline'></div>
			</div>
			
				<div class="m_search">
					<form name="searchform" class="form u-search-form">
						<input type="text" class="input u-search-input" placeholder="Search" />
						<span class="icon icon-search"></span>
					</form>
				</div>
			
			<ul class='switcher h-list'>
				
					<li class='s-search'><a href='javascript:void(0)'><span class="icon icon-search flat-box"></span></a></li>
				
				<li class='s-menu'><a href='javascript:void(0)'><span class="icon icon-menu flat-box"></span></a></li>
			</ul>
		</div>
		
		<div class='nav-sub container container--flex'>
			<a class="logo" class="flat-box" href='javascript:void(0)'>
				Word of Forks
			</a>

			<ul class='switcher h-list'>
				<li class='s-comment'><a href='javascript:void(0)'><span class="icon icon-chat_bubble_outline flat-box"></span></a></li>
				<li class='s-top'><a href='javascript:void(0)'><span class="icon icon-arrow_upward flat-box"></span></a></li>
				<li class='s-toc'><a href='javascript:void(0)'><span class="icon icon-format_list_numbered flat-box"></span></a></li>
			</ul>
		</div>
	</div>
</header>
<aside class="menu-phone">
	<nav>
		
			<a href="/" class="nav-Home nav">
				首页
			</a>
		
			<a href="/archives" class="nav-archives nav">
				归档
			</a>
		
			<a href="/categories" class="nav-categories nav">
				分类
			</a>
		
			<a href="/tags" class="nav-tags nav">
				标签
			</a>
		
			<a href="https://book.algorithm.ttxxly.top/" class="nav-algorithm nav">
				算法
			</a>
		
			<a href="https://book.python.ttxxly.top/" class="nav-python nav">
				python
			</a>
		
			<a href="/about" class="nav-about nav">
				关于
			</a>
		
	</nav>
</aside>

    <script>setLoadingBarProgress(40);</script>
  <div class="l_body">
    <div class='container clearfix'>
      <div class='l_main'>
        <article id="post-Java-数字格式化"
  class="post white-box article-type-post"
  itemscope itemprop="blogPost">
	<section class='meta'>
	<h2 class="title">
  	<a href="/2019/01/10/Java-数字格式化/">
    	Java 数字格式化
    </a>
  </h2>
	<time>
	  Jan 10, 2019
	</time>
	
	</section>
	
		<section class="toc-wrapper"></section>
	
	<section class="article typo">
  	<div class="article-entry" itemprop="articleBody">
    	<p>数字的格式化在解决实际问题时使用非常普遍，如表示某超市的商品价格，不要八六两位有效数字。Java 主要对浮点型数据进行数字格式化操作，其中浮点型数据包括 double 和 float 型数据，在 java 中使用 <code>Java.text.DecimalFormat</code>格式化数字。</p>
<p>DecimalFormat 是 NumberFormat 的一个子类，用于格式化十进制数字。他可以将一些数字格式化为整数、浮点数、百分数等。通过使用该类可以为要输出的数字加上单位或控制数字的精度。</p>
<p>DecimalFormat 类中特殊字符说明</p>
<p><img src="/images/2019-1-10/1.png" alt></p>
<p><strong>格式化数字的例子</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.text.DecimalFormat;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DecimalFormatSimpleDemo</span> </span>&#123;</span><br><span class="line">    <span class="comment">//使用实例化对象时设置格式化模式</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title">SimpleFormat</span><span class="params">(String pattern, <span class="keyword">double</span> value)</span>   </span>&#123;</span><br><span class="line">        <span class="comment">//实例化 DecimalFormat 对象</span></span><br><span class="line">        DecimalFormat myFormat = <span class="keyword">new</span> DecimalFormat(pattern);</span><br><span class="line">        String output = myFormat.format(value);<span class="comment">//将数字格式化</span></span><br><span class="line">        System.out.println(value+<span class="string">" "</span>+pattern+<span class="string">" "</span>+output):</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="comment">//使用 applyPattern() 方法对数字进行格式化</span></span><br><span class="line">    <span class="function"><span class="keyword">static</span> <span class="keyword">public</span> <span class="keyword">void</span> <span class="title">UseApplyPatternMethodFormat</span><span class="params">(String pattern, <span class="keyword">double</span> value)</span> </span>&#123;</span><br><span class="line">        DecimalFormat myFormat = <span class="keyword">new</span> DecimalFormat();</span><br><span class="line">        myFormat.applyPattern(pattern);</span><br><span class="line">        System.out.println(value+<span class="string">" "</span>+pattern+<span class="string">" "</span>+myFormat.format(value));</span><br><span class="line">    &#125;</span><br><span class="line">    </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        SimpleFormat(<span class="string">"###,###.###"</span>, <span class="number">123456.789</span>);</span><br><span class="line">        SImpleFormat(<span class="string">"00000000.###kg"</span>, <span class="number">123456.789</span>);</span><br><span class="line">        <span class="comment">//按照格式模式格式化数字，不存在的位以 0 显示</span></span><br><span class="line">        SimpleFormat(<span class="string">"000000.000"</span>, <span class="number">123.78</span>);</span><br><span class="line">        <span class="comment">//调用静态 UseApplyPatternMethodDormat()方法</span></span><br><span class="line">        UseApplyPatternMethodFormat(<span class="string">"#.###%"</span>, <span class="number">0.789</span>);</span><br><span class="line">        <span class="comment">//将小数点够格式化为两位</span></span><br><span class="line">        UseApplyPatternMethodFormat(<span class="string">"###.##"</span>, <span class="number">123456.789</span>);</span><br><span class="line">        <span class="comment">//将数字格式为 千分数形式</span></span><br><span class="line">        UseApplyPatternMethodFormat(<span class="string">"0.00\u2030"</span>, <span class="number">0.789</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在 DecimalFormat 类中除了可以设置格式化模式来格式化数字之外，还可以使用一些特殊方法对数字进行格式化设置。<br>例如分组。。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">Import java.text.DecimalFormat;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">DecimalMethod</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        DecimalFormat myFormat = <span class="keyword">new</span> DecimalFormat();</span><br><span class="line">        myFormat.setGroupingSize(<span class="number">2</span>);<span class="comment">//设置将数字每两个分一组</span></span><br><span class="line">        String output = myFormat.format(<span class="number">123456.789</span>);</span><br><span class="line">        System.out.println(<span class="string">"将数字以每两个数字分组"</span>+ output );</span><br><span class="line">        myFormat.setGroupingSize(<span class="keyword">false</span>);<span class="comment">//设置不允许分组</span></span><br><span class="line">        String output = myFormat.format(<span class="number">123456.789</span>);</span><br><span class="line">        System.out.println(<span class="string">"不允许数字分组"</span>+ output );</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
  	</div>
	  
	  <div class="article-tags tags">
      
        <a href="/tags/Java/">Java</a>
      
	  </div>
    
		
	
		<div class="art-item-footer">
				
					<span class="art-item-left"><i class="icon icon-chevron-thin-left"></i>prev：<a href="/2019/01/11/Java-数学运算/" rel="prev"  title="Java 数学运算">
						Java 数学运算 
					</a></span>
				
				
					<span class="art-item-right">next：<a href="/2019/01/09/java-包装类/" rel="next"  title="java 包装类">
						java 包装类
					</a><i class="icon icon-chevron-thin-right"></i></span>
				
		</div>
	
	</section>
	
</article>
<script>
	window.subData = {
		title: 'Java 数字格式化',
		tools: true
	}
</script>

      </div>
      <aside class='l_side'>
        
  <section class='m_widget links'>
<div class='header'>Links</div>
<div class='content'>
    <ul class="entry">
    
        <li><a class="flat-box" target="_blank" href="https://iiong.com/">
            <div class='name'>淮城一只猫</div>
        </a></li>
    
    </ul>
</div>
</section>

  <section class='m_widget categories'>
<div class='header'>Categories</div>
<div class='content'>
    
    <ul class="entry">
    
        <li><a class="flat-box" href="/categories/Android基础/"><div class='name'>Android基础</div><div class='badget'>1</div></a></li>
    
    </ul>
    
</div>
</section>

  
<div class="m_widget tagcloud">
    <div class="header">Tags</div>
    <div class='content'>
        <a href="/tags/Android/" style="font-size: 14px; color: #808080">Android</a> <a href="/tags/HTML/" style="font-size: 14px; color: #808080">HTML</a> <a href="/tags/Java/" style="font-size: 20px; color: #000">Java</a> <a href="/tags/Python/" style="font-size: 14px; color: #808080">Python</a>
    </div>
</div>



      </aside>
      <script>setLoadingBarProgress(60);</script>
    </div>
  </div>
  <footer id="footer" class="clearfix">

	<div class="social-wrapper">
  	
      
        <a href="https://github.com/stkevintan" class="social github"
          target="_blank" rel="external">
          <span class="icon icon-github"></span>
        </a>
      
        <a href="https://twitter.com/kevinsfork" class="social twitter"
          target="_blank" rel="external">
          <span class="icon icon-twitter"></span>
        </a>
      
        <a href="/atom.xml" class="social rss"
          target="_blank" rel="external">
          <span class="icon icon-rss"></span>
        </a>
      
    
  </div>
  
  <div>Theme <a href='https://github.com/stkevintan/hexo-theme-material-flow' class="codename">MaterialFlow</a> designed by <a href="http://keyin.me/" target="_blank">Kevin Tan</a>.</div>
  
</footer>


  <script>setLoadingBarProgress(80);</script>
  

<script src="//apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src='//cdn.bootcss.com/node-waves/0.7.5/waves.min.js'></script>
<script src="//cdn.bootcss.com/scrollReveal.js/3.3.2/scrollreveal.min.js"></script>
<script src="/js/jquery.fitvids.js"></script>
<script>
	var GOOGLE_CUSTOM_SEARCH_API_KEY = "";
	var GOOGLE_CUSTOM_SEARCH_ENGINE_ID = "";
	var ALGOLIA_API_KEY = "";
	var ALGOLIA_APP_ID = "";
	var ALGOLIA_INDEX_NAME = "";
  var AZURE_SERVICE_NAME = "";
  var AZURE_INDEX_NAME = "";
  var AZURE_QUERY_KEY = "";
  var BAIDU_API_ID = "";
  var SEARCH_SERVICE = "hexo";
  var ROOT = "/"||"/";
  if(!ROOT.endsWith('/'))ROOT += '/';
</script>
<script src="/js/search.js"></script>
<script src="/js/app.js"></script>


  <script>setLoadingBarProgress(100);</script>
</body>
</html>
